//
//  GHGiveViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/22.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHGiveViewController.h"
#import "GHGiveTableViewCell.h"
#import "GHGiveModel.h"
@interface GHGiveViewController ()
@property(nonatomic, strong) NSMutableArray<GHGiveModel*>                     *dataSouce;
@property(nonatomic, strong) STLabel                     *shenmaLable;
@end

@implementation GHGiveViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.dataSouce = [NSMutableArray new];
    self.title = @"赠送明细";
    [self configSubView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMygiveDetailRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
    headerView.backgroundColor =self.tableView.backgroundColor;
    
    STLabel * yueLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 30, UIScreenWidth - 20, 20)
                                                   text:[NSString stringWithFormat:@"￥%@ 余额",self.userModel
                                                         .u_money]
                                              textColor:GH_redColor
                                                   font:18
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentCenter];
    yueLable.attributedText = [yueLable.text st_convertAttributeStringWithKeyWord:@"余额" attributes:@{NSForegroundColorAttributeName:GH_secendTextColor,NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    [headerView addSubview:yueLable];
    
    STLabel * shenmaLable = [[STLabel alloc] initWithFrame:CGRectMake(10, yueLable.bottom, UIScreenWidth - 20, 20)
                                                      text:@""
                                                 textColor:GH_redColor
                                                      font:18
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentCenter];
    shenmaLable.attributedText = [shenmaLable.text st_convertAttributeStringWithKeyWord:@"累计赠送神马值" attributes:@{NSForegroundColorAttributeName:GH_secendTextColor,NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    self.shenmaLable = shenmaLable;
    [headerView addSubview:yueLable];
    [headerView addSubview:shenmaLable];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, shenmaLable.bottom + 20, UIScreenWidth, 70)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:whiteView];
    headerView.height = whiteView.bottom;
    self.tableView.tableHeaderView = headerView;
    
    UIControl * shenmazhi = [[GHMenuControl alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 3, 70)
                                                           value:[NSString stringWithFormat:@"%@个",self.userModel.wfpoints]
                                                            name:@"神马值"
                                                        showLine:YES];
    [whiteView addSubview:shenmazhi];
    
    UIControl * currentshenmazhi = [[GHMenuControl alloc] initWithFrame:CGRectMake(shenmazhi.right, 0, UIScreenWidth / 3, 70)
                                                                  value:[NSString stringWithFormat:@"￥%@",self.userModel.now_wfpoints]
                                                                   name:@"当前神马值"
                                                               showLine:YES];
    [whiteView addSubview:currentshenmazhi];
    
    UIControl * leijishenmazhi = [[GHMenuControl alloc] initWithFrame:CGRectMake(currentshenmazhi.right, 0, UIScreenWidth / 3, 70)
                                                                value:[NSString stringWithFormat:@"￥%@个",self.userModel.all_wfpoints]
                                                                 name:@"累计神马值"
                                                             showLine:YES];
    [whiteView addSubview:leijishenmazhi];
    
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GHGiveTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHGiveTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHGiveTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    GHGiveModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --NetWork Method
- (void)sendMygiveDetailRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/personal/account_details"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 //zensong 神马值
                                                 if ([dataDic.allKeys containsObject:@"grand_total"]) {
                                                     NSString * give = dataDic[@"grand_total"];
                                                     self.shenmaLable.text = [NSString stringWithFormat:@"￥%@ 累计赠送神马值",give];
                                                     _shenmaLable.attributedText = [_shenmaLable.text st_convertAttributeStringWithKeyWord:@"累计赠送神马值" attributes:@{NSForegroundColorAttributeName:GH_secendTextColor,NSFontAttributeName:[UIFont systemFontOfSize:13]}];
                                                 }
                                                 
                                                 if ([dataDic.allKeys containsObject:@"account_details"]) {
                                                     NSArray * array = dataDic[@"account_details"];
                                                     self.dataSouce = [NSMutableArray new];
                                                     for (NSDictionary * modelDic in array) {
                                                         GHGiveModel * model = [GHGiveModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             }];
}
@end

